/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.casestudy1.main;

import com.casestudy1.bus.SearchBus;
import com.casestudy1.bus.SearchNumber;
import com.casestudy1.bus.SearchStreet;
import com.casestudy1.view.AboutForm;
import com.casestudy1.view.BlockedTrafficForm;
import com.casestudy1.view.LoginForm;
import com.casestudy1.view.MainMenuForm;
import com.casestudy1.view.Preference;
import com.casestudy1.view.RegisterForm;
import com.casestudy1.view.Setting;
import com.casestudy1.view.SplashScreen;
import com.nokia.mid.ui.orientation.Orientation;
import com.nokia.mid.ui.orientation.OrientationListener;
import com.sun.lwuit.Display;
import com.sun.lwuit.Image;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;
import dat.Main;
import dat.lichchieuphim;
import dat.rapchieuphim;
import dat.thongtinrap;
import java.io.IOException;
import javax.microedition.media.MediaException;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;

/**
 * @author Windsea
 */
public class MainMidlet extends MIDlet implements OrientationListener {

    public Preference pref;
    public Resources res;
    public Resources theme;
    private SplashScreen spscr;
    private AboutForm abfrm;
    private Setting op;
    private LoginForm lgfrm;
    private RegisterForm regfrm;
    private MainMenuForm mainfrm;
    private BlockedTrafficForm blktffrm;
    //Form cua ĐẠT////////// 
    private Main main;
    private rapchieuphim rcp;
    private lichchieuphim lcp;
    private thongtinrap ttr;
    //////////////////////  
    //Tuan Anh
    private SearchBus searchBus;
    private SearchNumber searchNumber;
    private SearchStreet searchStreet;
    /////////////////////
    private String ip = "192.168.1.1";
    private String themename = "";

    /**
     * startApp
     */
    public void startApp() {
        try {
            pref = new Preference("user2");
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
        
       
        
        
       
        
        Orientation.addOrientationListener(this);
        showSplashAndInitialize();
        
        showLoginForm();

    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        Display.getInstance().exitApplication();
    }

    /**
     * Chuẩn bị hiển thị các thành phần
     */
    private void initializeDisplay() {
        Display.init(this);

    }

    public Image getImage(String name) {
        Image img = null;
        loadResources();
        img = res.getImage(name);
        return img;
    }

    /**
     * Hiển thị splashscreen
     */
    private void showSplashAndInitialize() {

        initializeDisplay();
        Display.getInstance().setForceFullScreen(true);
        String themename = getThemeRecordname();
        System.out.println("Theme:" +themename);
        if(themename != "Default Theme" || themename != null){
        loadTheme(themename);
        }
        
        spscr = new SplashScreen(this, getImage("splash"));   
        showSplashView();
        long start = System.currentTimeMillis();
        createViews();
        long difference = System.currentTimeMillis() - start;

        try {
            // Show splash view for 3 seconds
            Thread.sleep(Math.max(0, 3000 - difference));
        } catch (InterruptedException e) {
        }
        Display.getInstance().setForceFullScreen(false);
    }

    /**
     * Hiển thị forms
     */
    //Form cua ĐẠT//////////////
    public void showMainForm() {
        main.show();
    }

    public void showRCPForm() {
        rcp.show();
    }

    public void showLCPForm() {
        lcp.show();
    }

    public void showTTRForm() {
        ttr.show();
    }
    ////////////////////////////
    // Tuan Anh

    public void showSearchBus() {
        searchBus.show();
    }

    public SearchBus getSearchBus() {
        return searchBus;
    }

    public void showSearchStreet() {
        searchStreet.show();
    }

    public void showSearchNumber() {
        searchNumber.show();
    }
    ////////////////

    public void showLoginForm() {
        lgfrm.show();
    }

    public void showRegisterForm() {
        regfrm.show();
    }

    public void showMainMenu() {
        mainfrm.show();
    }

    public void showSplashView() {
        spscr.show();
    }

    public void showBlockedTrafficForm() {
        blktffrm.show();
    }

    public void showAboutForm() {
        abfrm.show();
    }

    public void showOptionForm() {
        op.show();
    }

    //get form
    public LoginForm getLoginForm() {
        return lgfrm;
    }

    public MainMenuForm getMainMenuForm() {
        return mainfrm;
    }

    public BlockedTrafficForm getBlockedTrafficForm() {
        return blktffrm;
    }

    public Setting getOptions() {
        return op;
    }

    public Main getMainForm() {
        return main;
    }

    public rapchieuphim getRCPForm() {
        return rcp;
    }

    public lichchieuphim getLCPForm() {
        return lcp;
    }

    public thongtinrap TTRForm() {
        return ttr;
    }

    public void createViews() {
        lgfrm = new LoginForm(this);
        mainfrm = new MainMenuForm(this);;
        regfrm = new RegisterForm(this);
        abfrm = new AboutForm(this);
        op = new Setting(this);
    }

    public void createViews2() {
        try {
            blktffrm = new BlockedTrafficForm(this);
            main = new Main(this);
            rcp = new rapchieuphim(this);
            ttr = new thongtinrap(this);
            lcp = new lichchieuphim(this);
            searchBus = new SearchBus(this);
            searchNumber = new SearchNumber(this);
            searchStreet = new SearchStreet(this);
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
    }

    public void loadResources() {
        try {
            res = Resources.open("/resources.res");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void loadTheme(String name) {
        try {
            theme = Resources.open("/theme.res");
            UIManager.getInstance().setThemeProps(theme.getTheme(name));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void refreshTheme() {
        UIManager.getInstance().getLookAndFeel().setReverseSoftButtons(true);
        Display.getInstance().getCurrent().refreshTheme();
        getLoginForm().refreshTheme();
        getOptions().refreshTheme();
    }

    public String getThemeName() {
        return themename;
    }

    public void setThemeName(String name) {
        this.themename = name;
    }

    public void setThemeRecord(String name){
        pref.put("theme", name);
        try {
            pref.save();
            System.out.println(pref.get(name));
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }
    public void displayOrientationChanged(int newDisplayOrientation) {
        switch (newDisplayOrientation) {
            case Orientation.ORIENTATION_PORTRAIT:
            case Orientation.ORIENTATION_PORTRAIT_180:
                Orientation.setAppOrientation(Orientation.ORIENTATION_PORTRAIT);
                break;
            case Orientation.ORIENTATION_LANDSCAPE:
            case Orientation.ORIENTATION_LANDSCAPE_180:
                Orientation.setAppOrientation(Orientation.ORIENTATION_LANDSCAPE);
                break;
        }
    }

    public String getIP() {
        return ip;
    }

    public void setIP(String ip) {
        this.ip = ip;
    }

    public void setUser(String username, String password) {
        pref.put(username, password);
        try {
            pref.save();
            System.out.println(pref.get(username));
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }

    public String getUser(String key) {
        return pref.get(key);
    }
    public String getThemeRecordname(){
        return pref.get("theme");
    }
}
